package com.example.administrator.nl.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.administrator.nl.R;
import com.example.administrator.nl.activity.SearchActivity;
import com.example.administrator.nl.fragment.shopping.CommodityFragment;
import com.example.administrator.nl.util.LogUtil;
import com.example.administrator.nl.util.ViewPageFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShoppingFragment extends Fragment {
    private String TAG = "ShoppingFragment";
    Unbinder unbinder;
    @BindView(R.id.ll_search_shopping)
    LinearLayout ll_search_shopping;

    @BindView(R.id.tab_shopping)
    TabLayout tab_shopping;

    @BindView(R.id.vp_shopping)
    ViewPager vp_shopping;

    @BindView(R.id.et_search_shopping)
    EditText et_search_shopping;

    public ShoppingFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        LogUtil.Log_D(TAG, "onCreateView");
        View view = inflater.inflate(R.layout.fragment_shopping, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        listener();
        return view;
    }

    private void init() {
        tab_shopping.addTab(tab_shopping.newTab().setText("精选"));
        tab_shopping.addTab(tab_shopping.newTab().setText("美容"));
        tab_shopping.addTab(tab_shopping.newTab().setText("包包"));
        tab_shopping.addTab(tab_shopping.newTab().setText("母婴"));
        List<Fragment> list = new ArrayList<>();

        CommodityFragment commodityFragment = new CommodityFragment();
        CommodityFragment commodityFragment2 = new CommodityFragment();
        CommodityFragment commodityFragment3 = new CommodityFragment();
        CommodityFragment commodityFragment4 = new CommodityFragment();

        Bundle bundle1 = new Bundle();
        bundle1.putString("type", "精选");
        commodityFragment.setArguments(bundle1);

        Bundle bundle2 = new Bundle();
        bundle2.putString("type", "美容");
        commodityFragment2.setArguments(bundle2);

        Bundle bundle3 = new Bundle();
        bundle3.putString("type", "包包");
        commodityFragment3.setArguments(bundle3);

        Bundle bundle4 = new Bundle();
        bundle4.putString("type", "母婴");
        commodityFragment4.setArguments(bundle4);

        list.add(commodityFragment);
        list.add(commodityFragment2);
        list.add(commodityFragment3);
        list.add(commodityFragment4);
        vp_shopping.setAdapter(new ViewPageFragmentAdapter(getActivity().getSupportFragmentManager(), list));
    }

    private void listener() {
        ll_search_shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });

        et_search_shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });

        tab_shopping.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        vp_shopping.setCurrentItem(0);
                        break;
                    case 1:
                        vp_shopping.setCurrentItem(1);
                        break;
                    case 2:
                        vp_shopping.setCurrentItem(2);
                        break;
                    case 3:
                        vp_shopping.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        vp_shopping.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        vp_shopping.setCurrentItem(0);
                        tab_shopping.getTabAt(0).select();
                        break;
                    case 1:
                        vp_shopping.setCurrentItem(1);
                        tab_shopping.getTabAt(1).select();

                        break;
                    case 2:
                        vp_shopping.setCurrentItem(2);
                        tab_shopping.getTabAt(2).select();

                        break;
                    case 3:
                        vp_shopping.setCurrentItem(3);
                        tab_shopping.getTabAt(3).select();

                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.Log_D(TAG, "onDestroy");
        if (null != unbinder) {
            unbinder.unbind();
        }
    }
}
